﻿<%@ Page Title="Quản lý khách hàng" Language="C#" MasterPageFile="~/Admin/Masters/AdminMasterPage.master" AutoEventWireup="true" CodeFile="CustomerManager.aspx.cs" Inherits="Admin_CustomerManager" %>

<%@ Register src="Parts/Pagging.ascx" tagname="Pagging" tagprefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Title" Runat="Server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="head" Runat="Server">
    <script type="text/javascript">
    $(document).ready(function () {        

        $('a[name="deleteone"]').click(function () {
            if (confirm('Bạn có chắc muốn xóa khách hàng này không?') == false) {
                return false;
            }
        });

        $('#btndelete').click(function () {
            if ($('input[name="MaKhachHang"]:checked').length <= 0) {
                alert("Bạn chưa chọn khách hàng cần xóa.");
                return false;
            } else {
                if (confirm('Bạn có chắc muốn xóa những khách hàng được chọn không?') == false) {
                    return false;
                } else {
                    $('#customer_manager').attr('action', $('#deletehidden').val());
                    $(this).submit();
                }
               
            }

            
        });
    });

</script>
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" Runat="Server">

<div class="content-box"><!-- Start Content Box -->
				
		<div class="content-box-header">
					
			<h3>Quản lý khách hàng</h3>
					
			<ul class="content-box-tabs">
				<li><a href="#tab1" class="default-tab">Danh sách</a></li>                
			</ul>
					
			<div class="clear"></div>
					
		</div> <!-- End .content-box-header -->
				
<div class="content-box-content">

<!-- Start Tab Danh Sach-->
<%
	          
              string act = "";
              int khId =0;
              bool isSuccess = false;
             
              if (Request.QueryString["act"] != null)
                  act = Request.QueryString["act"].ToString();
              if (Request.QueryString["id"] != null)
                  khId =Convert.ToInt32(Request.QueryString["id"].ToString());
             
              NhaKinhDoanhDataContext db = new NhaKinhDoanhDataContext();
             
              if (act != "" && act == "delete")
              {
                  if (khId > 0)
                  {
                      //xóa 1
                      if ((from kh in db.KhachHangs
                           where kh.MaKhachHang.Equals(khId)
                           select kh).Count() > 0)
                      {
                          var querykh = (from kh in db.KhachHangs
                                         where kh.MaKhachHang.Equals(khId)
                                         select kh).Single();
                         
                          db.KhachHangs.DeleteOnSubmit(querykh);
                          db.SubmitChanges();
                          isSuccess = true;
                         
                      }
                  }
                  else {
                      string MaKhachHangTemp = Request.Form["MaKhachHang"];
                      if (MaKhachHangTemp != null)
                      {
                          string[] MaKhachHangs = MaKhachHangTemp.Split(',');
                          foreach (string item in MaKhachHangs)
                          {
                              int ma = Convert.ToInt32(item.Trim());
                              if ((from kh in db.KhachHangs
                                   where kh.MaKhachHang.Equals(ma)
                                   select kh).Count() > 0)
                              {
                                  var querykh = (from kh in db.KhachHangs
                                                 where kh.MaKhachHang.Equals(ma)
                                                 select kh).Single();
                                  
                                  db.KhachHangs.DeleteOnSubmit(querykh);
                                  db.SubmitChanges();
                                  isSuccess = true;
                              }
                          }
                         
                      }
                  }
              }

              int trang = 1;
              if (Request.QueryString["Page"] != null)
                  trang = Convert.ToInt32(Request.QueryString["Page"].ToString());
              var query = from tl in db.KhachHangs select tl;
              List<KhachHang> lst = new List<KhachHang>();
              if (query.Count<KhachHang>() > 0)
              {
                  int nums = query.Count<KhachHang>();
                  Session["TongSoDongDuLieu"] = nums;
                  Session["LinkPhanTrang"] = "CustomerManager.aspx";
                  int soDongTrenTrang = Convert.ToInt32(Session["SoDongTrenTrang"]);
                  if (soDongTrenTrang <= 0)
                  {
                      soDongTrenTrang = 10;
                      Session["SoDongTrenTrang"] = 10;
                  }
                  int skip = (trang - 1) * soDongTrenTrang;
                  lst = query.Skip(skip).Take(soDongTrenTrang).ToList<KhachHang>();
              }
              else
              {
                  Session["TongSoDongDuLieu"] = 0;
                  Session["SoDongTrenTrang"] = 10;
              }      
               
              
%>
<div class="tab-content default-tab" id="tab1"> 
<!-- This is the target div. id must match the href of this div's tab -->
					
<!-- Start Notification -->
<% if (act != "" && isSuccess)
   {%>
<div class="notification <%if(isSuccess){%>success<%}else{%>attention<%}%> png_bg">
	<a href="javascript:void(0);" class="closeMessage">
        <img src="Images/icons/cross_grey_small.png" title="Đóng thông báo này" alt="Đóng" />
    </a>
	<div><% if (act == "delete")
         {%>
		   Xóa thành công khách hàng
	<%} %></div>
</div>
<%} %>
<!-- End Notification -->
						
	<table align="center" width="100%">							
		<thead>
        <tr>
				<td colspan="8">
					<div class="bulk-actions align-left">					
                        <input id="deletehidden" name="delete" type="hidden" value="CustomerManager.aspx?act=delete&Page=<%=trang%>"/>
                        <input id="btndelete" type="submit" value="Xóa khách hàng được chọn" class="button" />
					</div>		 
			
				</td>
			</tr>

			<tr>
				<th><input class="check-all" type="checkbox"  name="checkall"/>
                </th>
                <th>STT</th>
				<th>Họ tên</th>
				<th>CMND</th>
				<th>Ngày sinh</th>
				<th>Địa chỉ</th>				
                <th>Điện thoại</th>               
                <th>Tác vụ</th>             
			</tr>
								
		</thead>
						 
		<tfoot>
			<tr>
				<td colspan="8">					
										 
<!-- Start Pagging -->
<uc1:Pagging ID="Pagging1" runat="server" />
<!-- End Pagging -->
									
				</td>
			</tr>
		</tfoot>
						 
		<tbody>
          <% 
              int stt = 0;
           foreach (KhachHang kh in lst)
	      {
              stt++;
              StringBuilder account = new StringBuilder();
              if (kh.TaiKhoan != null)
                  account.AppendFormat("{0}   - {1}", kh.TaiKhoan.TenDangNhap, kh.TaiKhoan.NgayDangKy);
	      %>
			<tr>
				<td><input type="checkbox" name="MaKhachHang" value="<%=kh.MaKhachHang%>" /></td>
				<td><%=string.Format("{0:000}",stt) %></td>
                <td><a href="EditCustomer.aspx?act=edit&id=<%=kh.MaKhachHang%>&Page=<%=trang%>" title="<%=account %>"><%=MD5Hash.Trim(kh.TenKhachHang) %></a></td>
				<td><%=MD5Hash.Trim(kh.CMND) %></td>
				<td><%=MD5Hash.Trim(kh.NgaySinh.ToString()) %></td>
				<td><%=MD5Hash.Trim(kh.DiaChi) %></td>               
                <td><%=MD5Hash.Trim(kh.SoDienThoai) %></td>                              
               
				<td>
					    <a href="EditCustomer.aspx?act=view&id=<%=kh.MaKhachHang%>&Page=<%=trang%>" title="Xem chi tiết"><img src="Images/icons/details.png" alt="Xem chi tiết"/></a>
						<a href="EditCustomer.aspx?act=edit&id=<%=kh.MaKhachHang%>&Page=<%=trang%>" title="Cập nhật"><img src="Images/icons/pencil.png" alt="Cập nhật"/></a>
						<a name="deleteone" href="CustomerManager.aspx?act=delete&id=<%=kh.MaKhachHang%>&Page=<%=trang%>" title="Xóa"><img src="Images/icons/cross.png" alt="Xóa" /></a> 						
				</td>
			</tr>								
			
			<%}%>
		</tbody>							
	</table>						
</div>


<!-- End Tab Danh Sach-->					
  
   
					
</div> <!-- End .content-box-content -->
				
</div>

</asp:Content>